package com.tangtangtang.gulimall.thirdpaty.OssController;

import com.alibaba.fastjson.JSONObject;
import com.tangtang.common.utils.R;
import com.tangtangtang.gulimall.thirdpaty.TencentUtils.ThreadCosConfigUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@Slf4j
public class OssTencentController {

    @Autowired
    ThreadCosConfigUtils threadCosConfigUtils;

    @Value("${uploadfile.address}")
    private String multipartfile;

    //理解一下@RequestParam和@RequestBody两者作用的区别是，前者只是请求部分数据，不使用json格式数据，后者使用于json数据，
    // 但要绑定相应的实体类，能够获取全部数据的json格式的解析
    @RequestMapping("/getCosurl")
    public R Upload(HttpServletRequest request) {

        System.out.println("有上传请求进入预签名的请求！");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("获取当前的时间为：" + sdf.format(new Date()));
        //随机生成一个uuid进行预签名
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        String url = threadCosConfigUtils.getfileurluploadtoCos(uuid);
        System.out.println(url);
        return R.ok().put("data", url);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public R uploadtofileCos(@RequestBody MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        System.out.println("判断图片是否为空："+file.isEmpty());
        String getCosfileurl = null;//获取可以查看的连接
        //1.腾讯云上传第一步，得将文件进行转存
        String filename = file.getOriginalFilename();

        //2.获取文件的后缀名
        String suffix = filename.substring(filename.lastIndexOf(".")+1);
        //以时间命名，如果只以年月日命名会重复名称。还得加个uuid来一面重复
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        //这个是上传的文件名称
        String ToCosfilename = String.valueOf(sdf.format(new Date()))+"-"+uuid+"."+suffix;
        try {
            //3.转存地址
            String filepath = multipartfile+ToCosfilename;
            file.transferTo(new File(filepath));

            //4.转存成功以后，根据预签名进行上传
            String filetoCosUrl = threadCosConfigUtils.getfileurluploadtoCos(filepath);

            //5.根据预签名+转存的文件路径名称，进行文件上传,返回一个可以访问的路径
            getCosfileurl =  threadCosConfigUtils.uploadurltoCos(filetoCosUrl,filepath,ToCosfilename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().put("data",getCosfileurl);
    }
}
